package com.songyl.message.service;

import com.songyl.message.dao.MessageDao;
import com.songyl.message.entity.Message;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
@Service("MessageService")
public class MessageService {

    @Autowired
    private MessageDao messageDao;

    @Transactional(rollbackOn = Exception.class)
    public boolean send(Message message){
        Message s =messageDao.save(message);
        return  messageDao.exists(s.getId());
    }

    public List<Message>  findAll(Message  m){
        if(m ==null){
            m = new Message(0);
        }
        //1、设置过滤条件
        Example<Message> example = Example.of(m);
        //2、排序
        Sort  sort = new Sort(Sort.Direction.ASC, "id");
        //、排序、查询
        return messageDao.findAll(example,sort);
    }

    @Transactional(rollbackOn = Exception.class)
    public void delete(String  ids) {
        List<Message> messages = new ArrayList<>();
        String[] idArr = ids.split(",");
        List<Long> idList = new ArrayList<>(idArr.length);
        for (String id : idArr) {
            Message m = messageDao.getOne(Long.parseLong(id));
            m.setFlag(1);
            messages.add(m);
        }
        messageDao.save(messages);
    }
}
